import webSocket from '../../socket/socket.js'
import buildMessage from '../../utils/message-build.js'
import messageStorage from '../../utils/message-storage.js'

const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    messageValue: "",
    otherUserInfo: {},
    iUserInfo: {},
    messageRecord: [],
    scorllId: "item-id-10",
    socketConnected:true,

    socketListener:null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  
  onShow: function () {
    let  temp =  (e) => {
      if(e.msg=="close"){
        this.setData({
          socketConnected:false
        })
        wx.showModal({
          title:"提示",
          content:"服务器连接失败，请重新连接",
        })
      }else  if(e.msg=="connected"){
        this.setData({
          socketConnected:true
        })
      }
    };

    wx.event.on('socket-events',temp)
    this.setData({
      socketListener:temp,
      otherUserInfo: app.globalData.currentClickUserInfo,
      iUserInfo: app.globalData.myUserInfo
    })
    app.globalData.currentSessionOpenId = this.data.otherUserInfo.userInfo.id
    wx.setNavigationBarTitle({
      title: this.data.otherUserInfo.userInfo.userName,
    })

    var res = messageStorage.getStorageMessage(this.data.otherUserInfo.userInfo.id)
    this.setData({
      messageRecord:res,
      scorllId: "item-id-" + (res.length - 1),
    })

    /**
     * 监听他的消息
     */
    wx.event.on(this.data.otherUserInfo.userInfo.id, (e) => {
      var temp = this.data.messageRecord;
      temp.push(e)
      this.setData({
        messageRecord: temp,
        scorllId: "item-id-" + (temp.length - 1)
      })
    })

  },
  onMessageInput: function (e) {
    this.setData({
      messageValue: e.detail.value
    })
  },
  /**
   * 可能会话列表中不存在他/她，需要通知到上方。
   */
  onSendMessage: function () {
    if(!this.data.socketConnected){
      wx.showModal({
        title:"提示",
        content:"服务器断开连接，是否重新连接",
        success(res){
          if (res.confirm) {
            webSocket.connectServer()
          } else if (res.cancel) {
          }
        },
        cancelColor: 'cancelColor',
      })
    }
    var tempMessage = this.data.messageValue;
    this.setData({
      messageValue: ""
    })
    if (tempMessage == "") {
    } else {
      var otherUserInfo = this.data.otherUserInfo;
      var iUserInfo = this.data.iUserInfo;
      var newMessageItem = messageStorage.createMessageItem(tempMessage, true);
      messageStorage.storageMessage(otherUserInfo.userInfo.id, newMessageItem)
      webSocket.sendMessage(buildMessage.buildForwordMessage(iUserInfo.id, otherUserInfo.userInfo.id, tempMessage))
      var tempRecord = this.data.messageRecord;
      tempRecord.push(newMessageItem);

      this.setData({
        scorllId: "item-id-" + (tempRecord.length - 1),
        messageRecord: tempRecord,
        messageValue: ""
      })
    }
    /**
    * 通知上方
    */
    wx.event.emit("create-session", { "fromId": otherUserInfo.userInfo.id, "msg": tempMessage })

  },


  onUnload: function () {
    app.globalData.currentSessionOpenId = "",
    wx.event.off("socket-events",this.data.socketListener)

  },

})